/**
 * Created with IntelliJ IDEA.
 * Description: leetcode: LCR 047. 二叉树剪枝
 * <a href="https://leetcode.cn/problems/pOCWxh/description/">...</a>
 * User: DELL
 * Date: 2023-11-02
 * Time: 21:33
 */

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    /**
     * 解题思路: (递归)
     * 本题主要采用深度优先遍历，即遍历每一颗子树，若满足剪切条件，则返回
     * null，若不满足剪切条件，则将当前子树的根节点返回即可。
     * 就这样进行一遍深度优先遍历，即可将整个数剪切完毕。
     *
     * @param root
     * @return
     */
    public TreeNode pruneTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        root.left = pruneTree(root.left);
        root.right = pruneTree(root.right);
        if (root.val == 0 && root.left == null && root.right == null) {
            return null;
        }
        return root;
    }
}
